package club.hicode.daydayup.sslFile.server;

import club.hicode.daydayup.sslFile.util.SSLMODE;
import club.hicode.daydayup.sslFile.util.SecureChatSslContextFactory;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.ssl.SslHandler;

import javax.net.ssl.SSLEngine;


/**
 * Creates a newly configured {@link ChannelPipeline} for a new channel.
 */
public class SecureChatServerInitializer extends ChannelInitializer<SocketChannel> {

	private String tlsMode;

	public SecureChatServerInitializer(String TLS_MODE) {
		tlsMode = TLS_MODE;
	}

	@Override
	public void initChannel(SocketChannel ch) throws Exception {

		// Add SSL handler first to encrypt and decrypt everything.
		// In this example, we use a bogus certificate in the server side
		// and accept any invalid certificates in the client side.
		// You will need something more complicated to identify both
		// and server in the real world.
		//
		// Read SecureChatSslContextFactory
		// if you need client certificate authentication.

		//		SSLEngine engine = null;
		//		if (SSLMODE.CA.toString().equals(tlsMode)) {
		//		    engine = SecureChatSslContextFactory
		//			    .getServerContext(
		//				    tlsMode,
		//				    System.getProperty("user.dir")
		//					    + "/src/sslFile/keystore/client/sChat.jks",
		//				    null).createSSLEngine();
		//		} else if (SSLMODE.CSA.toString().equals(tlsMode)) {
		//		    engine = SecureChatSslContextFactory
		//			    .getServerContext(
		//				    tlsMode,
		//				    System.getProperty("user.dir")
		//					    + "/src/sslFile/keystore/twoway/sChat.jks",
		//				    System.getProperty("user.dir")
		//					    + "/src/sslFile/keystore/twoway/sChat.jks")
		//			    .createSSLEngine();
		//
		//		} else {
		//		    System.err.println("ERROR : " + tlsMode);
		//		    System.exit(-1);
		//		}
		//		engine.setUseClientMode(false);
		//
		//		// Client auth
		//		if (SSLMODE.CSA.toString().equals(tlsMode))
		//		    engine.setNeedClientAuth(true);
		//
		////		System.out.println("报告");
		////        System.out.println("信息：有一客户端链接到本服务端");
		////        System.out.println("IP:" + ch.localAddress().getHostName());
		////        System.out.println("Port:" + ch.localAddress().getPort());
		////        System.out.println("报告完毕");
		//
		//
		//		pipeline.addLast("ssl", new SslHandler(engine));

		// On top of the SSL handler, add the text line codec.
		//pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192,
		//	Delimiters.lineDelimiter()));
		//pipeline.addLast("decoder", new StringDecoder());
		//pipeline.addLast("encoder", new StringEncoder());

		// and then business logic.
		//pipeline.addLast("handler", new SecureChatServerHandler());


		ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(2048576, 0, 4, 0, 4));
		// ch.pipeline().addLast(new ByteArrayDecoder());
		// 在管道中添加我们自己的接收数据实现方法
		ch.pipeline().addLast(new MyServerHandler_Nio());
	}
}
